package com.sangu.dianxiu.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.sangu.dianxiu.BaseApp;
import com.sangu.dianxiu.ICallBack;
import com.sangu.dianxiu.R;
import com.sangu.dianxiu.bean.user.ContactEntity;
import com.sangu.dianxiu.utils.UserManager;

import org.simple.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;

/**
 * 常用地址adapter，继承自AppBaseAdapter
 * Created by sg2 on 2016/1/11.
 */
public class FrequentContactsAdapter extends AppBaseAdapter<ContactEntity.ContactsEntity>{
    private UserManager userManager=new UserManager();
    public FrequentContactsAdapter(List<ContactEntity.ContactsEntity> list, Context context) {
        super(list, context);
    }

    @Override
    public ViewHolder getViewHolder(int position, View convertView, ViewGroup parent) {
        final ContactEntity.ContactsEntity entity=list.get(position);
        ViewHolder vh=ViewHolder.get(context,convertView,parent, R.layout.item_frequent_contacts_listview);
        TextView tvName= (TextView) vh.findViewById(R.id.tv_item_frequent_contact_name);
        TextView tvDefault= (TextView) vh.findViewById(R.id.tv_item_frequent_contact_default);
        TextView tvPhone= (TextView) vh.findViewById(R.id.tv_item_frequent_contact_phone);
        ImageView ivDelete= (ImageView) vh.findViewById(R.id.iv_item_frequent_contact_delete);
        tvName.setText(entity.getName());
        tvPhone.setText(entity.getPhone());
        if(!entity.isIsDefault())
        {
            tvDefault.setVisibility(View.GONE);     //如果不是默认地址就让默认地址标志隐藏
        }
        ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String,String> map=new HashMap<String, String>();
                map.put("userId", BaseApp.user.getUserId()+"");
                map.put("contactId",entity.getId()+"");
                userManager.deleteContacts(map, new ICallBack() {       //删除联系人要同事更新联系人列表，使用notifyDataSetChanged();但是FrequentContactActivity中的相应的adapter要注册观察者才能生效
                    @Override
                    public void onSuccess(String string) {
                        notifyDataSetChanged();
                    }

                    @Override
                    public void onError(VolleyError error) {

                    }
                });
            }
        });
        return vh;
    }
}
